<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="icon" th:href="@{/img/logo.ico}" href="../static/img/logo.ico" type="images/x-ico"/>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="../static/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}">
    <title>公告列表</title>
    <link rel="stylesheet" th:href="@{/fontawesome/css/font-awesome.min.css}"
          href="../../static/fontawesome/css/font-awesome.min.css">

    <link rel="stylesheet"
          href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,500;0,600;0,700;1,400&display=swap">

    <!-- Fontawesome CSS -->
    <link rel="stylesheet" href="../static/fontawesome/css/fontawesome.min.css"
          th:href="@{/fontawesome/css/fontawesome.min.css}">
    <link rel="stylesheet" href="../static/fontawesome/css/all.min.css" th:href="@{/fontawesome/css/all.min.css}">

    <!-- Main CSS -->
    <link rel="stylesheet" href="../static/css/back_index.css" th:href="@{/css/back_index.css}">
    <style type="text/css">
        table {
            table-layout: fixed;
        }

        td {
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }
    </style>
</head>
<body>
<div class="main-wrapper">
    <div th:replace="commons/bar::#menubar(activeUri1='公告列表')"></div>

    <div class="page-wrapper">
        <div class="content container-fluid">
            <div class="row">
                <div class="col-lg-12">
                    <h1>公告列表</h1>
                    <hr>
                    <div class="form-inline col-sm-8">
                        <label class="control-label col-sm-2" for="level"><strong>公告等级</strong></label>
                        <select id="level" class="form-control form-control-sm col-sm-4"
                                data-live-search="true">
                            <option value="" selected="selected">请选择</option>
                            <option value="1">紧急公告</option>
                            <option value="2">重要公告</option>
                            <option value="3">普通公告</option>
                        </select>
                    </div>
                    <hr>
                    <div class="fixed-table-toolbar">
                        <div class="bs-bars pull-left">
                            <div id="toolbar" class="btn-group">
                                <button type="button" class="btn btn-primary btn-sm" data-toggle="modal"
                                        data-target="#addNoticeModal">
                                    <span class="fa fa-plus" aria-hidden="true"></span> 发布公告
                                </button>
                                <button type="button" class="btn btn-danger btn-sm" data-toggle="modal"
                                        id="deleteCheckBtn" disabled="disabled">
                                    <span class="fa fa-plus" aria-hidden="true"></span> 删除选中
                                </button>
                            </div>
                        </div>
                        <div class="columns columns-right btn-group pull-right">
                            <div class="keep-open btn-group" title="列">
                                <button class="btn btn-default" type="button" name="refresh" title="刷新"
                                        onclick="myrefresh()">
                                    <i class="fa fa-refresh"></i></button>
                                <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                                    <i class="fas fa-table"></i><span class="caret"></span>
                                </button>
                                <ul class="dropdown-menu" role="menu">
                                    <li>
                                        <input type="checkbox" id="td1" onclick="hiddenTD('td1')">编号
                                    </li>
                                    <li>
                                        <input type="checkbox" checked="checked" id="td2" onclick="hiddenTD('td2')">等级
                                    </li>
                                    <li>
                                        <input type="checkbox" checked="checked" id="td3" onclick="hiddenTD('td3')">内容
                                    </li>
                                    <li>
                                        <input type="checkbox" checked="checked" id="td4" onclick="hiddenTD('td4')">操作
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="fixed-table-container">
                        <table class="table table-hover table-striped table-bordered" id="datas">
                            <thead>
                            <th style="max-width: 10px"><input type="checkbox" class="checkall"
                                                               onclick="mydeleteallcheck(this.checked)"></th>
                            <th class="td1" hidden="hidden">编号</th>
                            <th class="td2">等级</th>
                            <th class="td3">内容</th>
                            <th class="td4">操作</th>
                            </thead>
                            <tbody id="mytbody">
                            <tr id="template">
                                <td class="td0"></td>
                                <td class="td1" hidden="hidden"></td>
                                <td class="td2"></td>
                                <td class="td3"></td>
                                <td class="td4"></td>
                            </tr>
                            </tbody>
                        </table>
                        <br/>
                        <div>
                            <div class="pull-left">
                                <input type="hidden" id="offset" value="0">
                                显示第<span id="begin"></span>到第<span id="end"></span>条记录，总共<span id="sum"></span>条记录。
                                每页显示
                                <select id="limit" onchange="this.allNoticeIds = changeLimit(this.allNoticeIds)">
                                    <option value="5">5</option>
                                    <option value="10" selected="selected">10</option>
                                    <option value="15">15</option>
                                    <option value="30">30</option>
                                    <option value="50">50</option>
                                    <option value="100">100</option>
                                </select> 条记录
                            </div>
                            <div class="pull-right">
                                <nav aria-label="Page navigation">
                                    <ul class="pagination" id="pages"></ul>
                                </nav>
                            </div>
                        </div>
                    </div>
                </div>
                <div id="updateNoticeModal" class="modal fade" tabindex="-1" role="dialog"
                     aria-labelledby="myLargeModalLabel"
                     data-backdrop="static" data-keyboard="false">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title"
                                    id="noticeModalLabel">详细信息</h5>
                                <button type="button" class="close" data-dismiss="modal"
                                        aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                <table align="center" id="updateTable">
                                    <input type="hidden" id="noticeid"/>
                                    <tr>
                                        <td>等级</td>
                                        <td>
                                            <select name="level">
                                                <option class="myselect" value="3">普通公告</option>
                                                <option class="myselect" value="2">重要公告</option>
                                                <option class="myselect" value="1">紧急公告</option>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>内容</td>
                                        <td>
                                            <textarea rows="8" cols="24" name="noticeInfo" id="noticeInfo"></textarea>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <div class="modal-footer">
                                <button type="button" id="submitUpdate" class="btn btn-info">
                                    保存修改
                                </button>
                                <button type="button" id="submitDelete" class="btn btn-danger">
                                    删除公告
                                </button>
                                <button type="button" class="btn btn-secondary"
                                        data-dismiss="modal">
                                    关闭
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <div id="addNoticeModal" class="modal fade" tabindex="-1" role="dialog"
                     aria-labelledby="myLargeModalLabel"
                     data-backdrop="static" data-keyboard="false">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title">发布公告</h5>
                                <button type="button" class="close" data-dismiss="modal"
                                        aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                <table align="center">
                                    <tr>
                                        <td>等级</td>
                                        <td>
                                            <select name="level">
                                                <option class="addselect" value="3" selected="selected">普通公告</option>
                                                <option class="addselect" value="2">重要公告</option>
                                                <option class="addselect" value="1">紧急公告</option>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>内容</td>
                                        <td>
                                            <textarea rows="8" cols="24" name="noticeInfo"
                                                      id="addNoticeInfo"></textarea>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <div class="modal-footer">
                                <button type="button" id="submitAdd" class="btn btn-info">
                                    确定发布
                                </button>
                                <button type="button" class="btn btn-secondary"
                                        data-dismiss="modal">
                                    关闭
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <footer>
                    <p>2020 &copy; 软件学院学生信息管理系统 by 郑焙元</p>
                </footer>
            </div>
        </div>
    </div>
</div>
<script src="../static/js/jquery-3.5.1.min.js" th:src="@{/js/jquery-3.5.1.min.js}"></script>
<script>
    var allNoticeIds = [];

    var limit;
    var begin = 1;
    var end = 0;
    var pageNum;
    var currentPage = 1;

    $(function () {
        limit = $("#limit option:selected").val();
        TableInit();
        var buttonGroup = new ButtonGroup();
        buttonGroup.init();
    });

    var TableInit = function () {
        sendAjax('/admin/api/notice', 'get', {
            limit: $("#limit option:selected").val(),
            offset: $("#offset").val(),
            level: $("#level option:selected").val()
        }, function (res) {
            paging(res);
            $.each(res.data, function (i, notice) {
                var row = $("#template").clone();
                row.find(".td0").html('<input type="checkbox" class="checks" onclick="mydeletecheck(' + notice.noticeid + ')">');
                row.find(".td1").text(notice.noticeid);
                let level = notice.level == "3" ? "普通公告" : notice.level == "2" ? "重要公告" : "紧急公告";
                row.find(".td2").text(level);
                let color = notice.level == "3" ? "color: black" : notice.level == "2" ? "color: #ff6600" : "color: red";
                row.find(".td2").attr("style", color);
                row.find(".td3").text(notice.noticeinfo);
                let html = '<button type="button" class="btn btn-info btn-sm" data-toggle="modal" ' +
                    'data-target="#updateNoticeModal" onclick="updateNotice(' + notice.noticeid + ')">详情</button>';
                row.find(".td4").html(html);
                row.appendTo("#datas");//添加到模板的容器中
            });
            $("#template:first").remove();
        }, '');
    }

    function myrefresh() {
        $("#mytbody").children().remove();
        $("#mytbody").html('<tr id="template">\n' +
            '                                <td class="td0"></td>\n' +
            '                                <td class="td1" hidden="hidden"></td>\n' +
            '                                <td class="td2"></td>\n' +
            '                                <td class="td3"></td>\n' +
            '                                <td class="td4"></td>\n' +
            '                            </tr>');
        TableInit();
        hiddenTD("td1");
    }

    function updateNotice(noticeid) {
        sendAjax('/admin/api/notice/' + noticeid, 'get', {}, function (res) {
            $("#noticeid").val(res.data.noticeid);
            $(".myselect").each(function () {
                if ($(this).val() === res.data.level) {
                    $(this).prop("selected", true);
                }
            });
            $("#noticeInfo").text(res.data.noticeinfo);
        }, '');
    }

    var ButtonGroup = function () {
        var object = {};

        object.init = function () {
            $('#submitAdd').click(function () {
                if (confirm('确认发布公告？')) {
                    sendAjax('/admin/api/notice', 'post', {
                        level: $('.addselect:selected').val(),
                        noticeinfo: $('#addNoticeInfo').val(),
                    }, function (res) {
                        alert("发布成功！");
                        $('#addNoticeModal').modal('hide');
                        myrefresh();
                    }, '#submitAdd');
                }
            });

            $("#submitUpdate").click(function () {
                sendAjax('/admin/api/notice', 'put', {
                    noticeid: $("#noticeid").val(),
                    level: $(".myselect:selected").val(),
                    noticeinfo: $("#noticeInfo").val()
                }, function (res) {
                    alert("修改成功！");
                    $('#updateNoticeModal').modal('hide');
                    myrefresh();
                }, '#submitUpdate');
            });

            $('#submitDelete').click(function () {
                if (confirm('确认删除公告？')) {
                    sendAjax('/admin/api/notice/' + $("#noticeid").val(), 'delete', {}, function (res) {
                        alert("删除成功！");
                        $('#updateNoticeModal').modal('hide');
                        myrefresh();
                    }, '#submitDelete');
                }
            });

            $('#deleteCheckBtn').click(function () {
                if (confirm('确认删除选中公告？')) {
                    sendAjax('/admin/api/notice/list', 'delete', {
                        allNoticeIds: JSON.stringify(allNoticeIds),
                    }, function (res) {
                        alert("删除成功！");
                        allNoticeIds = [];
                        $(".checkall").prop("checked", false);
                        $("#deleteCheckBtn").prop("disabled", "disabled");
                        myrefresh();
                    }, '#deleteCheckBtn');
                }
            });

        };

        return object;
    };


    function mydeleteallcheck(noticeid) {
        this.allNoticeIds = deleteallcheck(noticeid, allNoticeIds);
    }

    function mydeletecheck(noticeid) {
        this.allNoticeIds = deletecheck(noticeid, allNoticeIds);
    }
</script>
<script src="../static/js/popper.min.js" th:src="@{/js/popper.min.js}"></script>
<script src="../static/js/bootstrap.min.js" th:src="@{/js/bootstrap.min.js}"></script>
<script src="../static/js/jquery.slimscroll.min.js" th:src="@{/js/jquery.slimscroll.min.js}"></script>
<script src="../static/js/script.js" th:src="@{/js/script.js}"></script>

</body>
</html>
